package com.ruoyi.web.controller.system;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TenancyType;
import com.ruoyi.system.service.ITenancyTypeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 租户类型Controller
 * 
 * @author ruoyi
 * @date 2023-03-03
 */
@RestController
@RequestMapping("/system/type")
public class TenancyTypeController extends BaseController
{
    @Autowired
    private ITenancyTypeService tenancyTypeService;

    /**
     * 查询租户类型列表
     */
//    @PreAuthorize("@ss.hasPermi('system:type:list')")
    @GetMapping("/list")
    public TableDataInfo list(TenancyType tenancyType)
    {

        if(!"sys".equalsIgnoreCase(getUserTenancyCode())){
            tenancyType.setTenancyCode(getUserTenancyCode());
        }
        startPage();
        List<TenancyType> list = tenancyTypeService.selectTenancyTypeList(tenancyType);
        return getDataTable(list);
    }

    /**
     * 导出租户类型列表
     */
    @PreAuthorize("@ss.hasPermi('system:type:export')")
    @Log(title = "租户类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TenancyType tenancyType)
    {
        List<TenancyType> list = tenancyTypeService.selectTenancyTypeList(tenancyType);
        ExcelUtil<TenancyType> util = new ExcelUtil<TenancyType>(TenancyType.class);
        util.exportExcel(response, list, "租户类型数据");
    }

    /**
     * 获取租户类型详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:type:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tenancyTypeService.selectTenancyTypeById(id));
    }

    /**
     * 新增租户类型
     */
    @PreAuthorize("@ss.hasPermi('system:type:add')")
    @Log(title = "租户类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TenancyType tenancyType)
    {
        tenancyTypeService.checkTenancyTypeAllowed(tenancyType);
        tenancyType.setCreatedAt(new Date());
        tenancyType.setCreatedBy(getUsername());
        return toAjax(tenancyTypeService.insertTenancyType(tenancyType));
    }

    /**
     * 修改租户类型
     */
    @PreAuthorize("@ss.hasPermi('system:type:edit')")
    @Log(title = "租户类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TenancyType tenancyType)
    {
        return toAjax(tenancyTypeService.updateTenancyType(tenancyType));
    }

    /**
     * 删除租户类型
     */
    @PreAuthorize("@ss.hasPermi('system:type:remove')")
    @Log(title = "租户类型", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tenancyTypeService.deleteTenancyTypeByIds(ids));
    }
}
